<template>
  <div class="player-parent">
    <div
      @mouseup="handleMouseUp"
      class="player-box"
      :style="{
        background: props.background,
      }"
    >
      <div class="left-box" @click.stop="play">
        <i :style="{ color: props.iconColor }" class="doorIcon" v-if="!isPlay">&#xe672;</i>
        <i :style="{ color: props.iconColor }" class="doorIcon" v-else>&#xe671;</i>
        <div class="left-box-shadow" :style="{ background: props.playColor }"></div>
      </div>
      <div class="player-box-right">
        <div class="right-top">
          <span class="audio-name" :style="{ color: props.nameColor }">{{ getName() }}</span>
          <div class="right-top-menu">
            <!-- <div class="right-top-play" @click.stop="infoShow = !infoShow">
            <i :style="{ color: props.iconColor }" class="doorIcon">&#xe667;</i>
          </div> -->
            <t-popup
              v-if="!isMobile"
              content="查看讲解词"
              delay="10"
              placement="top"
              class="placement top center"
              trigger="hover"
              attach="hoverIcon"
            >
              <div class="right-top-play" @click.stop="infoShow = !infoShow" id="hoverIcon">
                <i :style="{ color: props.iconColor }" class="doorIcon">&#xe667;</i>
              </div>
            </t-popup>
            <div v-else class="right-top-play" @click.stop="infoShow = !infoShow" id="hoverIcon">
              <i :style="{ color: props.iconColor }" class="doorIcon">&#xe667;</i>
            </div>
          </div>
        </div>
        <div class="right-bottom">
          <div class="right-bottom-left" :style="{ color: props.timeColor }">{{ realMusicTime }}</div>
          <div class="right-bottom-center" @mousedown="handleMouseDown">
            <t-slider
              v-if="!isMobile"
              @change-end="handleMouseUp"
              :label="false"
              :showStep="false"
              :tooltipProps="{ placement: 'bottom' }"
              v-model="percentage"
              @change="change"
              style="width: 100%"
            />
            <van-slider
              v-else
              v-model="percentage"
              @drag-end="handleMouseUp"
              @drag-start="dragStart"
              style="width: 100%"
            />
          </div>
          <div class="right-bottom-right" :style="{ color: props.timeColor }">{{ totalMusicTime }}</div>
        </div>
      </div>
    </div>
    <div
      class="infoShow-box"
      :style="{
        background: props.background,
        color: props.descriptionColor,
      }"
      v-if="infoShow && props.description"
    >
      {{ props.description }}
    </div>
  </div>
  <audio :src="urls + props.url" ref="audioRef" v-show="false" @ended="ended" />
</template>

<script setup lang="ts">
// @ts-nocheck
import { urls } from '@/utils/request/upload';
import { ref, onMounted, nextTick, reactive, computed } from 'vue';

const emit = defineEmits(['delAudioUrl']);
const isMobile = navigator.userAgent.match(
  /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i,
);
const audioRef = ref(null);
const isPlay = ref(false);
const infoShow = ref(false);
const realMusicTime = ref('00:00');
const totalMusicTime = ref('00:00');
const percentage = ref(0);
const move = ref(false);
const downClickType = ref(false);
const moveTop = ref(true);
const timer = ref(null);
const playBtmShow = ref(false);
// 进度条请使用样式穿透
const props = defineProps({
  url: {
    type: String,
  },
  description: {
    type: String,
  },
  name: {
    type: String,
  },
  nameColor: {
    //标题颜色
    type: String,
    default: 'rgba(0, 0, 0, 0.8)',
  },
  timeColor: {
    //事件颜色
    type: String,
    default: 'rgba(38, 41, 51, 1)',
  },
  iconColor: {
    //图标颜色
    type: String,
    default: '#262933',
  },
  background: {
    //背景颜色
    type: String,
    default: '#FFFFFF',
  },
  description: {
    type: String,
  },
  descriptionColor: {
    //讲解词颜色
    type: String,
    default: '#FFFFFF',
  },
  playColor: {
    type: String,
    default: '#FFFFFF',
  },
  //   audio: {
  //     type: Object,
  //   },
});

// default: {
//       type: 3,
//       url: '/369462942949445/audio/20230829/455210145833029.mp3',
//       description: '',
//       name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
//     },
const audio = reactive({
  type: 3,
  url: '/369462942949445/audio/20230829/455210145833029.mp3',
  description: '',
  name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
});
const handleMouseDown = () => {
  console.log('鼠标按下');
  move.value = true;
  downClickType.value = true;
};
const handleMouseUp = () => {
  if (!move.value) {
    move.value = false;
    return;
  }
  handClickBar();
  move.value = false;
  // if (move.value) {
  //   clearTimeout(timer.value);

  // }
  // console.log('鼠标抬起');
  // setTimeout(() => {
  //   move.value = false;
  // }, 300);
};
const dragStart = () => {
  move.value = true;
};
// 处理进度条
const getName = () => {
  if (!props.name) {
    return '';
  }
  let str = props.name.split('.mp3');
  // console.log(str);
  return str[0];
};
const ended = () => {
  console.log('播放完成');
  isPlay.value = false;
};
const del = () => {
  emit('delAudioUrl');
};
const handMusicBar = () => {
  let duration = audioRef.value.duration;
  let x = ((audioRef.value.currentTime / duration) * 100).toFixed(1);
  if (move.value) {
    clearTimeout(timer.value);
    return;
  } else {
    timer.value = setTimeout(() => {
      percentage.value = Number(x);
    }, 200);
  }

  // console.log(x, '当前播放百分比');
};

// 处理时间
const handlMusicTime = () => {
  //用秒数来显示当前播放进度
  let timeDisplay = Math.floor(audioRef.value.currentTime); //获取实时时间
  //分钟
  let minute = parseInt(timeDisplay / 60);
  if (minute < 10) {
    minute = '0' + minute;
  }
  //秒
  let second = Math.round(timeDisplay % 60);
  if (second < 10) {
    second = '0' + second;
  }
  realMusicTime.value = minute + ':' + second;
};

const watchMusicTime = () => {
  let music = audioRef.value;

  music.addEventListener(
    'timeupdate',
    () => {
      handlMusicTime();
      nextTick(() => {
        handMusicBar();
      });
    },
    false,
  );
  // 播放完毕

  // 捕获音频文件已准备完毕
  // 当媒体文件可以播放的时候会触发oncanplay事件,也可以用oncanplay
  music.oncanplaythrough = () => {
    let time = audioRef.value.duration;
    //分钟
    let minutes = parseInt(time / 60);
    if (minutes < 10) {
      minutes = '0' + minutes;
    }
    //秒
    let seconds = Math.round(time % 60);
    if (seconds < 10) {
      seconds = '0' + seconds;
    }
    totalMusicTime.value = minutes + ':' + seconds;
  };
};
// 处理点击进度条事件
const handClickBar = (e) => {
  audioRef.value.currentTime = (percentage.value / 100) * audioRef.value.duration; // 计算播放时间 位置百分比*总时间
  nextTick(() => {
    audioRef.value.play();
    isPlay.value = true;
  });
};
const change = () => {};
const play = () => {
  if (!playBtmShow.value) {
    playBtmShow.value = true;
  }
  if (audioRef.value.paused) {
    audioRef.value.play();
    isPlay.value = true;
  } else {
    audioRef.value.pause();
    isPlay.value = false;
  }
};
onMounted(() => {
  watchMusicTime();
});
</script>

<style scoped lang="less">
.player-parent {
  width: 100%;
  border-radius: 8px;
}
.player-box {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 118px;
  border-radius: 8px;
  padding: 24px 30px;
}
.left-box {
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  // background: rgba(244, 205, 117, 0.5);
  margin-right: 20px;
  border-radius: 6px;
  .doorIcon {
    font-size: 28px;
    position: relative;
    z-index: 12;
  }
}
.left-box-shadow {
  position: absolute;
  top: 0;
  left: 0;
  width: 80px;
  height: 80px;
  border-radius: 6px;
}
.box {
  animation: scroll 5s linear infinite;
}
.player-box-right {
  display: flex;
  flex-direction: column;
  // flex: 1;
  width: calc(100% - 100px);
  padding-right: 4px;
}
.right-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .audio-name {
    max-width: 70%;
    font-size: 24px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.8);
    line-height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.right-top-menu {
  display: flex;
  align-items: center;
  .doorIcon {
    font-size: 16px;
    line-height: 16px;
  }
  .leftIcon {
    cursor: pointer;
    color: rgba(0, 0, 0, 0.38);
  }
}
.right-top-play {
  display: flex;
  margin-left: 16px;
  .doorIcon {
    font-size: 28px;
    line-height: 28px;
    cursor: pointer;
    transform: rotate(90deg);
  }
}
.right-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 16px;
}
.right-bottom-left {
  font-size: 20px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(38, 41, 51, 1);
  line-height: 32px;
}
.right-bottom-center {
  margin: 0 20px;
  flex: 1;
  :deep(.t-slider__rail) {
    background: rgba(255, 255, 255, 0.2);
  }
}
.right-bottom-right {
  font-size: 20px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(38, 41, 51, 1);
  line-height: 32px;
}
:deep(.van-slider__button) {
  width: 16px;
  height: 16px;
}
.infoShow-box {
  width: 100%;
  border-radius: 8px;
  padding: 16px 30px;
  text-indent: 2em;
  color: #f4cd75;
  text-align: justify;
  font-family: 'PingFangSC, PingFang SC Microsoft YaHei';
  font-size: 16px;
  font-weight: 400;
  line-height: 32px; /* 200% */
  margin-top: 10px;
}
.right-top-menu {
  :deep(.t-popup) {
    color: rgba(0, 0, 0, 0.59);
    width: 104px;
    height: 40px;
    font-family: SourceHanSerifCN, SourceHanSerifCN;
    text-align: center;
    line-height: 24px;
    border-radius: 6px;
  }
}
</style>
